package org.chen.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.chen.domain.CrawlerTask;
import org.chen.service.CrawlerService;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;
import java.util.UUID;

@Slf4j
//@Configuration
//@EnableScheduling
@RequiredArgsConstructor
public class SchedulerConfig {

    private final List<CrawlerService> crawlerServices;
    
    @Value("${crawler.max-articles}")
    private int maxArticles;
    
//    @Scheduled(cron = "${crawler.sites.soccerodd.cron}")
    public void scheduleSocceroddCrawler() {
        crawlerServices.stream()
                .filter(service -> "soccerodd".equals(service.getSourceName()) && service.isSourceAvailable())
                .findFirst()
                .ifPresent(service -> {
                    log.info("Running scheduled SoccerOdd crawler task");
                    CrawlerTask task = CrawlerTask.builder()
                            .taskId(UUID.randomUUID().toString())
                            .source("soccerodd")
                            .category("soccerodd")
                            .targetCount(maxArticles)
                            .scheduled(true)
                            .build();
                    
                    try {
                        service.crawl(task);
                    } catch (Exception e) {
                        log.error("Scheduled SoccerOdd crawler task failed", e);
                    }
                });
    }
    
//    @Scheduled(cron = "${crawler.sites.freesupertips.cron}")
    public void scheduleFreeSuperTipsCrawler() {
        crawlerServices.stream()
                .filter(service -> "freesupertips".equals(service.getSourceName()) && service.isSourceAvailable())
                .findFirst()
                .ifPresent(service -> {
                    log.info("Running scheduled FreeSuperTips crawler task");
                    CrawlerTask task = CrawlerTask.builder()
                            .taskId(UUID.randomUUID().toString())
                            .source("freesupertips")
                            .category("freesupertips")
                            .targetCount(maxArticles)
                            .scheduled(true)
                            .build();
                    
                    try {
                        service.crawl(task);
                    } catch (Exception e) {
                        log.error("Scheduled FreeSuperTips crawler task failed", e);
                    }
                });
    }
}